/*
 * Copyright (c) 2015, the Dart project authors.
 *
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package vm.element

// This file is generated by the script: pkg/vm_service/tool/generate.dart in dart-lang/sdk.

import com.google.gson.JsonObject

/**
 * An {@link FieldRef} is a reference to a {@link Field}.
 */
class FieldRef(json: JsonObject) : ObjRef(json) {

    /**
     * The declared type of this field.
     *
     * The value will always be of one of the kinds: Type, TypeRef, TypeParameter, BoundedType.
     */
    fun getDeclaredType(): InstanceRef {
        return InstanceRef(json.get("declaredType").asJsonObject)
    }

    /**
     * The location of this field in the source code.
     *
     * Note: this may not agree with the location of `owner` if this is a field from a mixin
     * application, patched class, etc.
     *
     * Can return <code>null</code>.
     */
    fun getLocation(): SourceLocation? {
        val obj = json.get("location")?.asJsonObject ?: return null
        val type = json.get("type")?.asString
        if ("Instance" == type || "@Instance" == type) {
            val kind = json.get("kind")?.asString
            if ("Null" == kind) return null
        }
        return SourceLocation(obj)
    }


    /**
     * The owner of this field, which can be either a Library or a Class.
     *
     * Note: the location of `owner` may not agree with `location` if this is a field from a mixin
     * application, patched class, etc.
     */
    fun getOwner(): ObjRef {
        return ObjRef(json.get("owner").asJsonObject)
    }

    /**
     * Is this field const?
     */
    fun isConst(): Boolean {
        return getAsBoolean("const")
    }

    /**
     * Is this field final?
     */
    fun isFinal(): Boolean {
        return getAsBoolean("final")
    }

    /**
     * Is this field static?
     */
    fun isStatic(): Boolean {
        return getAsBoolean("static")
    }


}
